//
//  ConfigManager.m
//  WisdomZhuHai
//
//  Created by yewei on 13-10-12.
//  Copyright (c) 2013年 wyd. All rights reserved.
//

#import "ConfigManager.h"
#import "Common.h"

@interface ConfigManager()

@property (nonatomic,retain) NSDictionary *configDic;
@property id <ConfigManagerDelegate> configDelegate;
@property (nonatomic,retain) NSMutableDictionary *globalValueDic;
@property (nonatomic,retain) NSDictionary *defaultValueDic;

@end

@implementation ConfigManager

ConfigManager *sharedConfigManager= nil;

+ (ConfigManager*)shareInstance
{
    if (sharedConfigManager == nil)
    {
        sharedConfigManager = [[ConfigManager alloc] init];
    }
    return sharedConfigManager;
}

-(id)init
{
    self = [super init];
    if(self)
    {
        self.configDic = nil;
        _configDelegate = nil;
        self.globalValueDic = nil;
        self.defaultValueDic = [[JsonDataManager shareInstance] getDefaultConfigValueDicByJsonString:[FileManager readUtf8StringWithFilePath:[FileManager getFullResoucePath:@"common/defaultValue.json"]]];
        NSLog(@"default value = %@",self.defaultValueDic);
    }
    return self;
}

-(void)dealloc
{
    [_globalValueDic release];
    [_configDic release];
    [super dealloc];
}
-(void)loadConfigWithDelegate:(id <ConfigManagerDelegate> )delegate
{
    self.configDelegate = delegate;
    //版本号
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey];
    NSString *url = [NSString stringWithFormat:@"%@?platform=ios&app_version=%@",CONFIG_URL,version];
    [[HttpRequestManager getSharedInstance] getHttpInfo:[Common getUrlByString:url] withUserInfo:nil Delegate:self];
}

-(NSArray *)getTopNewsDicArray
{
    if([self.configDic count] > 0)
    {
        return [self.configDic objectForKey:TOPS];
    }
    return nil;
}


-(NSString *)getGlobalValueWithKey:(NSString *)key
{
    if(self.globalValueDic == nil)
    {
        self.globalValueDic = [NSMutableDictionary dictionary];
        if([self.configDic count] > 0)
        {
            NSArray *array =  [self.configDic objectForKey:GLOBAL_VALUES];
            for(NSDictionary *dic in array)
            {
                NSString *tempKey = [dic objectForKey:KEY];
                [self.globalValueDic setValue:[dic objectForKey:VALUE] forKey:tempKey];
            }
        }
         NSLog(@"global_values = %@",self.globalValueDic);
    }
    NSString *result = [GETVALUEBYKEY(key, self.globalValueDic) length] > 0 ? [self.globalValueDic objectForKey:key] : [self.defaultValueDic objectForKey:key];
    return result;
}


#pragma mark httpRequest delegate
-(void)requestDidFail:(ASIHTTPRequest *)request
{
    [[CustomStatueBar shareInstance] showStatusMessage:NSLocalizedString(@"Net unreachable",nil)];
    if(self.configDelegate && [self.configDelegate respondsToSelector:@selector(configDidLoadWithSuccess:)])
    {
        [self.configDelegate configDidLoadWithSuccess:NO];
    }
}

-(void)requestDidFinish:(ASIHTTPRequest *)request
{
    NSString *str = request.responseString;
//    str = [FileManager readUtf8StringWithFilePath:[FileManager getFullResoucePath:@"test/config.json"]];
    self.globalValueDic = nil;
    self.configDic = [[JsonDataManager shareInstance] getConfigDicByJsonString:str];
    if(self.configDelegate && [self.configDelegate respondsToSelector:@selector(configDidLoadWithSuccess:)])
    {
        [self.configDelegate configDidLoadWithSuccess:YES];
    }
}

@end
